/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-16 10:45:56
 * @LastEditTime : 2020-01-09 10:21:20
 * @LastEditors  : Please set LastEditors
 */
var DB = function () {
    return {
        dropTable:function() {
            var db = api.require('db');
            db.executeSql({
                name: 'APP',
                sql: 'drop table Chat'
            }, function (ret, err) {
                if (ret.status) {
                    alert('删除成功')
                }
            });
        },
        initDatabase:function(callback) {
            var db = api.require('db');
            db.openDatabase({
                name: 'APP'
            }, function (ret, err) {
                if (ret.status) {
                    //数据库创建成功
                    if (callback) callback();
                } else {
                    //初始化失败
                    layer.open({
                        content: '初始化失败 请稍后重试',
                        skin: 'msg',
                        time: 2
                    });
                }
            });
        },
        createTable:function(callback) {
            var db = api.require('db');
            //创建草稿表
            db.selectSql({
                name: 'APP',
                sql: 'select * from sqlite_master where type="table" and name = "Draft"'
            }, function (ret, err) {
                
                if (ret.status) {
                    if (!ret.data.length) {
                        db.executeSql({
                            name: 'APP',
                            sql: 'create table Draft(DraftID integer primary key autoincrement, JSONStr1 text, JSONStr2 text, JSONStr3 text, JSONStr4 text, CreateTime datetime, UpdateTime datetime, Type int, Status int, Remark varchar(255))'
                        }, function (ret, err) {
                            if (ret.status) {
                                //表创建成功
                                if (callback) callback();
                            }
                        });
                    }
                }
            });
            //创建聊天表
            db.selectSql({
                name: 'APP',
                sql: 'select * from sqlite_master where type="table" and name = "Chat"'
            }, function (ret, err) {
                if (ret.status) {
                    if (!ret.data.length) {
                        db.executeSql({
                            name: 'APP',
                            sql: 'create table Chat(ChatID integer primary key autoincrement, UserID int, JSONStr1 text, JSONStr2 text, JSONStr3 text, JSONStr4 text, CreateTime datetime, UpdateTime datetime, Type int, Status int, Remark varchar(255))'
                        }, function (ret, err) {
                            if (ret.status) {
                                //表创建成功
                                if (callback) callback();
                            }
                        });
                    }
                }
            });
        },
        selectSql:function(sql, callback) {
            var db = api.require('db');
            db.selectSql({
                name: 'APP',
                sql: sql
            }, function (ret, err) {
                if (ret.status) {
                    if (callback) callback(ret.data)
                }
            });
        },
        executeSql:function(sql, callback) {
            var db = api.require('db');
            db.executeSql({
                name: 'APP',
                sql: sql
            }, function (ret, err) {
                if (ret.status) {
                    if (callback) callback()
                } else {
                    alert(JSON.stringify(err))
                }
            });
        },
        selectSqlSync:function(sql) {
            var db = api.require('db');
            var sqlResult = db.selectSqlSync({
                name: 'APP',
                sql: sql
            });
            if (sqlResult.status) {
                return sqlResult.data;
            } else {
                return null;
            }
        },
        executeSqlSync:function(sql) {
            var db = api.require('db');
            
            var sqlResult = db.executeSqlSync({
                name: 'APP',
                sql: sql
            });
            if (sqlResult.status) {
                return sqlResult.data;
            } else {
                return null;
            }
        }

    }
}()